<template>
  <div class="app-container project-planning-container">
    <div v-show="type == 0">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="项目编号" prop="projectNumber">
          <el-input v-model="queryParams.projectNumber" placeholder="请输入项目编号" clearable @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item label="项目名称" prop="projectName">
          <el-input v-model="queryParams.projectName" placeholder="请输入项目名称" clearable @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>

      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>

      <el-table v-loading="loading" :data="planningList">
        <el-table-column label="项目编号" align="center" prop="projectNumber" />
        <el-table-column label="项目名称" align="center" prop="projectName">
          <template slot-scope="scope">
            <el-button type="text" @click="handleClick(scope.row)">{{ scope.row.projectName }}</el-button>
          </template>
        </el-table-column>
        <el-table-column label="项目负责人" align="center" prop="projectLeader" />
        <el-table-column label="计划开始时间" align="center" prop="plannedStartTime" width="180" />
        <el-table-column label="计划结束时间" align="center" prop="plannedEndTime" width="180" />
        <el-table-column label="审核状态" align="center" prop="auditState">
          <template slot-scope="scope">
            <el-tag
              :type="scope.row.auditState === '0' ? 'info' :
                     scope.row.auditState === '1' ? 'warning' :
                     scope.row.auditState === '2' ? 'success' :
                     scope.row.auditState === '3' ? 'danger' : ''"
              size="mini"
            >
              {{ scope.row.auditState === '0' ? '草稿' :
                 scope.row.auditState === '1' ? '审核中' :
                 scope.row.auditState === '2' ? '已审核' :
                 scope.row.auditState === '3' ? '已驳回' : '未知' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-if="scope.row.auditState === '0' || scope.row.auditState === '3'"
            >修改</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-if="scope.row.auditState === '0'"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
        @pagination="getList" />
    </div>

    <div v-if="type == 1">
      <PlanningForm
        :opt="opt"
        :id="id"
        @change="closeForm"
      />
    </div>
  </div>
</template>

<script>
import { listProjectPlanning, deleteProjectPlanning } from "@/api/project/planning";
import PlanningForm from "./form";

export default {
  name: "Planning",
  dicts: ['audit_state'],
  components: { PlanningForm },
  data() {
    return {
      // 遮罩层
      loading: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 项目策划表格数据
      planningList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 15,
        projectNumber: null,
        projectName: null
      },
      // 页面状态
      type: 0,
      // 操作类型
      opt: '',
      // 当前记录ID
      id: null
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询项目策划列表 */
    getList() {
      this.loading = true;
      listProjectPlanning(this.queryParams).then(response => {
        this.planningList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.type = 1;
      this.opt = "add";
      this.id = null;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.type = 1;
      this.opt = "edit";
      this.id = row.planningId;
    },
    /** 项目名称点击操作 */
    handleClick(row) {
      this.type = 1;
      this.opt = "view";
      this.id = row.planningId;
    },
    /** 表单关闭回调 */
    closeForm() {
      this.type = 0;
      this.getList();
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$modal.confirm('是否确认删除项目策划编号为"' + row.projectNumber + '"的数据项？').then(() => {
        return deleteProjectPlanning(row.planningId);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    }
  }
};
</script>

<style scoped lang="scss">
.project-planning-container {
  overflow-y: auto;
  overflow-x: auto;
  padding: 16px;
  max-width: 100%;
  max-height: 90vh;
  box-sizing: border-box;
}

.search-form {
  margin-bottom: 20px;
}
.mb8 {
  margin-bottom: 8px;
}
</style>
